import React, { useState, useEffect } from 'react';
import { Form, Button, Space } from 'antd';
import { goBack, componentsMap } from '@/utils/util';
import API from '@/api/cyindex.js';
import FileList from '@/components/FileLists';

const safety_unit_check = ({ history }) => {
  const [form] = Form.useForm();
  const [AQSJTB_SJLX, setAQSJTB_SJLX] = useState([]);
  const [SJLY, setSJLY] = useState([]);
  const [JJCD, setJJCD] = useState([]);
  const [fileList, setFileList] = useState([]);
  const [disposeFileList, setDisposeFileList] = useState([]);
  const receiverList = {};
  const TableList = [
    {
      label: '通报函号',
      name: 'reportId',
      type: 'input',
      otherProps: {
        disabled: true,
      },
    },
    {
      label: '事件来源',
      name: 'eventSource',
      type: 'select',
      otherProps: {
        disabled: true,
        options: SJLY,
      },
    },
    {
      label: '事件类型',
      name: 'eventType',
      type: 'select',
      otherProps: {
        disabled: true,
        options: AQSJTB_SJLX,
      },
    },
    {
      label: '紧急程度',
      name: 'emergency',
      type: 'select',
      otherProps: {
        disabled: true,
        options: JJCD,
      },
    },
    {
      label: '接收单位',
      name: 'receiverName',
      type: 'input',
      isremark: true,
      otherProps: {
        disabled: true,
      },
    },
  ];
  const TableList1 = [
    {
      label: '处置单位',
      name: 'receive',
      type: 'input',
      otherProps: {
        disabled: true,
      },
    },
    {
      label: '处置时间',
      name: 'updatetime',
      type: 'input',
      otherProps: {
        disabled: true,
      },
    },
    {
      label: '处置人',
      name: 'disUserName',
      type: 'input',
      otherProps: {
        disabled: true,
      },
    },
    {
      label: '处置说明',
      name: 'securityDispose_adress',
      type: 'textarea',
      isremark: true,
      otherProps: {
        disabled: true,
      },
    },
  ];
  const initList = () => {
    API.dictItemList()
      .then((res) => {
        const JJCDList = [];
        const SJLYList = [];
        const AQSJTB_SJLXList = [];
        if (res) {
          res.t.map((item) => {
            if (item.dictCode == 'JJCD') {
              JJCDList.push({
                label: item.dictItemName,
                value: item.dictItemType,
              });
            }
            if (item.dictCode == 'SJLY') {
              SJLYList.push({
                label: item.dictItemName,
                value: item.dictItemType,
              });
            }
            if (item.dictCode == 'AQSJTB_SJLX') {
              AQSJTB_SJLXList.push({
                label: item.dictItemName,
                value: item.dictItemType,
              });
            }
          });
          setJJCD(JJCDList);
          setSJLY(SJLYList);
          setAQSJTB_SJLX(AQSJTB_SJLXList);
        }
      })
      .then(() => {
        API.showServiceProviderList().then((res) => {
          if (res) {
            res.t.map((item) => {
              //接收单位
              receiverList[item.id] = item.name;
            });
          }
        });
      })
      .then(() => {
        const x = JSON.parse(history.location.query.xx);
        API.showDanWei({ id: x.tid, receiverId: x.receiverId }).then((res) => {
          if (res) {
            form.setFieldsValue({
              ...res.t,
              securityDispose_adress: res.t.securityDispose.adress,
              receive: receiverList[res.t.securityDispose.receiverId],
              updatetime: res.t.securityDispose.updateTime,
              disUserName: res.t.securityDispose.disUserName,
            });
            setFileList(res.t.snfileList);
            setDisposeFileList(res.t.disposeFileList);
          }
        });
      });
  };
  useEffect(() => {
    initList();
  }, []);
  return (
    <div className="main-x">
      <Form form={form}>
        <div className="base-title">安全事件信息</div>
        <div className="ant-form-inline form-x bgf">
          {TableList.map((item) => {
            return (
              <Form.Item
                key={item.name}
                label={item.label}
                name={item.name}
                className={item.isremark ? 'block' : ''}
              >
                {componentsMap(item.type, item.otherProps, true)}
              </Form.Item>
            );
          })}
          <Form.Item label="附件信息" className="block">
            {
              <FileList
                fileList={fileList}
                downloadUrl="/notification/dispose/download"
              />
            }
          </Form.Item>
        </div>
        <div className="base-title mt20">处置情况</div>
        <div className="ant-form-inline form-x bgf">
          {TableList1.map((item) => {
            return (
              <Form.Item
                key={item.name}
                label={item.label}
                name={item.name}
                className={item.isremark ? 'block' : ''}
              >
                {componentsMap(item.type, item.otherProps, true)}
              </Form.Item>
            );
          })}
          <Form.Item label="附件信息" className="block">
            {
              <FileList
                fileList={disposeFileList}
                downloadUrl="/notification/dispose/download"
              />
            }
          </Form.Item>
        </div>
        <div className="footer-x">
          <Space size={18}>
            <Button type="primary" className="cancel-btn" onClick={goBack}>
              返回
            </Button>
          </Space>
        </div>
      </Form>
    </div>
  );
};

export default safety_unit_check;
